
package com.oahcfly.chgame.egret.ui;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;

public class FntParser {

	// {"file":"snum.png","frames":{
	// "0":{"x":1,"y":1,"w":20,"h":24,"offX":2,"offY":0},
	// "1":{"x":106,"y":1,"w":11,"h":24,"offX":6,"offY":0},
	// "2":{"x":45,"y":1,"w":19,"h":24,"offX":2,"offY":0},
	// "3":{"x":86,"y":1,"w":18,"h":24,"offX":4,"offY":0},
	// "4":{"x":1,"y":27,"w":20,"h":24,"offX":2,"offY":0},
	// "5":{"x":66,"y":27,"w":18,"h":24,"offX":3,"offY":0},
	// "6":{"x":45,"y":27,"w":19,"h":24,"offX":2,"offY":0},
	// "7":{"x":66,"y":1,"w":18,"h":24,"offX":3,"offY":0},
	// "8":{"x":23,"y":1,"w":20,"h":24,"offX":2,"offY":0},
	// "9":{"x":23,"y":27,"w":20,"h":24,"offX":2,"offY":0}}}

	private HashMap<String, Fnt> fntMap = new HashMap<String, Fnt>();

	public String fntPngPath;

	public FntParser (String path) {

		FileHandle fileHandle = Gdx.files.internal(path);

		JsonReader jsonReader = new JsonReader();
		JsonValue jsonValue = jsonReader.parse(fileHandle);
		int count = jsonValue.size;
		fntPngPath = "";
		for (int i = 0; i < count; i++) {
			JsonValue child = jsonValue.get(i);
			if ("file".equals(child.name)) {
				fntPngPath = fileHandle.parent() + "/" + child.asString();
			} else if ("frames".equals(child.name)) {
				// System.err.println("object:" + child.size);
				int size = child.size;
				for (int j = 0; j < size; j++) {
					JsonValue fntValue = child.get(j);
					Fnt fnt = new Fnt();
					fnt.setFile(fntPngPath);
					fnt.setX(fntValue.getInt("x"));
					fnt.setY(fntValue.getInt("y"));
					fnt.setW(fntValue.getInt("w"));
					fnt.setH(fntValue.getInt("h"));
					fnt.setOffX(fntValue.getInt("offX"));
					fnt.setOffY(fntValue.getInt("offY"));
					// fnt.printInfo();
					fntMap.put(fntValue.name, fnt);
				}

			}
		}
	}

	public Fnt getFntInfo (String name) {
		return fntMap.get(name);
	}

}
